<template>
<div class="page-wrapper">
  <Breadcrumb v-bind="breads" />
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-body">
            <h6 class="card-title">已分配</h6>
            <template v-for="(role, index) in adminRoles"> <span :key="index" class="btn btn-outline-info">{{platformText(role.platform)}}-{{role.name}}</span>
            <a href="javascript:void(0);" :key="index" class="btn-link far fa-trash-alt" v-on:click="delAdminRole(index)"></a> </template>
          </div>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-body">
            <form class="form-inline" role="form" @submit.prevent="searchRoles(1)">
              <div class="form-group">
                <select id="search-platform" class="selectpicker" title="-平台类型-" @change="search.platform = $event.target.value">
                  <option value="">-平台类型-</option>
                  <option v-for="(plat, index) in platforms" :key="index" :value="plat.name">{{plat.text}}</option>
                </select>
              </div>
              <div class="form-group">
                <label class="control-label">角色名称：</label>
                <div class="btn-group">
                  <input type="text" v-model="search.name" class="form-control" style="width:150px;">
                </div>
              </div>
              <div class="form-group">
                <label class="control-label">角色编码：</label>
                <div class="btn-group">
                  <input type="text" v-model="search.code" class="form-control" style="width:150px;">
                </div>
              </div>
              <div class="form-group">
                <label class="control-label">&nbsp;</label>
                <button type="submit" id="search-bt" class="btn btn-primary">查 找</button>
              </div>
            </form>
          </div>
          <div class="col-12 table-responsive">
            <table class="table table-hover">
              <col width="25%">
              <col width="25%">
              <col width="25%">
              <col width="25%">
              <thead>
                <tr>
                  <th>角色名称</th>
                  <th>角色编码</th>
                  <th>平台类型</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="(role, index) in roles" :role="role" :index="index" :key="role.roleId">
                  <td>{{role.name}}</td>
                  <td>{{role.code}}</td>
                  <td>{{platformText(role.platform)}}</td>
                  <td>
                    <div class="custom-control custom-checkbox mr-sm-2">
                      <input type="checkbox" class="custom-control-input" :value="role.roleId" :id="'role' + role.roleId" v-model="adminRoleIds">
                      <label class="custom-control-label" :for="'role' + role.roleId">选择</label>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
            <div class="col-12">
              <PageLink v-bind="paging" v-on:go-page="searchRoles($event)" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <Footer />
</div>
</template>
<script>
  import $ from 'jquery'
  import cfg from '../dist/js/config'
  import Footer from './ComFooter'
  import Breadcrumb from './ComBreadcrumb'
  import PageLink from './ComPageLink'

  import { platforms } from '../dist/js/enum'

  const platform = -1

  // 查询条件
  const search = {}

  // 列表数据
  const roles = []

  // 分页信息
  const paging = {}

  // 已分配的角色
  const adminRoleIds = []
  const adminRoles = []

  function searchRoles(nowPage) {
    if (nowPage) {
      this.search.nowPage = nowPage
    }
    $.get(cfg.serverPath + '/role/page/' + this.platform + '/!/' + this.platType + '-set-role',
      this.search,
      result => {
        if (result.status != 0) {
          $(document).failMsg(result.message)
          return
        }
        Object.assign(this.paging, this.paging, result.data.paging)
        this.roles.splice(0, this.roles.length)
        if (!result.data.list) {
          return
        }
        for (let role of result.data.list) {
          this.roles.push(role) }
      })
  }
  function platformText(platform) {
    return platforms[platform].text
  }
  function loadAdminRoles() {
    this.adminRoles.splice(0, this.adminRoles.length)
    this.adminRoleIds.splice(0, this.adminRoleIds.length)
    $.get(cfg.serverPath + '/adminRole/list/role/' + this.platform + '/!/' + this.platType + '-set-role',
      {
        'admin.adminId': this.adminId,
        'role.platform': this.platforms[this.platform].name
      },
      result => {
        if (result.status != 0) {
          $(document).failMsg(result.message)
          return
        }
        for (let adminRole of result.data) {
          this.adminRoles.push(adminRole.role)
          this.adminRoleIds.push(adminRole.role.roleId) }
      })
  }
  function addAdminRole(role) {
    $.ajax({
      contentType: 'application/json; charset=UTF-8',
      url: cfg.serverPath + '/adminRole/add/' + this.platform + '/!/' + this.platType + '-set-role',
      type: 'put',
      data: JSON.stringify({
        id: {
          adminId: this.adminId,
          roleId: role.roleId
        }
      }),
      success: result => {
        if (result.status != 0) {
          $(document).failMsg(result.message)
          return
        }
        this.adminRoles.push(role)
      }
    })
  }
  function delAdminRole(index) {
    $.ajax({
      contentType: 'application/json; charset=UTF-8',
      url: cfg.serverPath + '/adminRole/del/' + this.platform + '/!/' + this.platType + '-set-role',
      type: 'delete',
      data: JSON.stringify({
        adminId: this.adminId,
        roleId: this.adminRoles[index].roleId
      }),
      success: result => {
        if (result.status != 0) {
          $(document).failMsg(result.message)
          return
        }
        this.adminRoles.splice(index, 1)
      }
    })

  }
  function addAdminRoleByIds() {
    for (let role of this.roles) {
      for (let roleId of this.adminRoleIds) {
        if (role.roleId == roleId) {
          let exists = false
          for (let adminRole of this.adminRoles) {
            if (adminRole.roleId == roleId) {
              exists = true
              break
            } }
          if (!exists) {
            this.addAdminRole(role)
          }
        } } }
  }
  function delAdminRoleByIds() {
    for (let i in this.adminRoles) {
      let adminRole = this.adminRoles[i]
      var exists = false
      for (let roleId of this.adminRoleIds) {
        if (adminRole.roleId == roleId) {
          exists = true
          break
        } }
      if (!exists) {
        this.delAdminRole(i)
      }
    }
  }
  function delAdminRoleIdByAdminRole() {
    for (let i in this.adminRoleIds) {
      let roleId = this.adminRoleIds[i]
      var exists = false
      for (let adminRole of this.adminRoles) {
        if (adminRole.roleId == roleId) {
          exists = true
          break
        } }
      if (!exists) {
        this.adminRoleIds.splice(i, 1)
      }
    }
  }
  export default {
    props: {
      platType: String,
      adminId: String
    },
    components: {
      Footer,
      Breadcrumb,
      PageLink
    },
    data() {
      return {
        platforms,
        platform,
        search,
        roles,
        paging,
        adminRoleIds,
        adminRoles
      }
    },
    methods: {
      searchRoles,
      platformText,
      loadAdminRoles,
      addAdminRole,
      delAdminRole,
      addAdminRoleByIds,
      delAdminRoleByIds,
      delAdminRoleIdByAdminRole
    },
    computed: {
      breads() {
        return {
          title: '角色分配',
          items: [
            {
              href: '#/',
              name: '主页'
            }, {
              href: '',
              name: '账号管理'
            },
            {
              href: '',
              name: '角色分配'
            } ]
        }
      }
    },
    created() {
      for (let i in this.platforms) {
        if (this.platforms[i].name.toLowerCase().indexOf(this.platType) != -1) {
          this.platform = i
          break
        }
      }
      this.search = {
        nowPage: 1
      }
      this.searchRoles()
      this.loadAdminRoles()
      $(function() {
        $('#search-platform').selectpicker()
      })
    },
    watch: {
      adminRoleIds(newIds, oldIds) {
        if (newIds.length > oldIds.length) {
          this.addAdminRoleByIds()
        } else {
          this.delAdminRoleByIds()
        }
      },
      adminRoles() {
        this.delAdminRoleIdByAdminRole()
      }
    }
  }
</script>
<style scoped>
.btn-outline-info {
	border: 0px;
}

.btn-outline-info:hover {
	color: #2255a4;
	background-color: transparent;
	border-color: #2255a4;
}

.custom-checkbox .custom-control-label::before {
	top: 0.16rem;
}

.custom-checkbox .custom-control-label::after {
	top: 0.16rem;
}

.custom-checkbox .custom-control-label {
	font-weight: 100;
	position: initial;
}
</style>